package cn.sharp.bbs.service.impl;

import cn.sharp.bbs.common.Utils;
import cn.sharp.bbs.common.page.PageParam;
import cn.sharp.bbs.dao.gen.GroupMapper;
import cn.sharp.bbs.dao.user.UserDao;
import cn.sharp.bbs.dao.user.UserFollowDao;
import cn.sharp.bbs.entity.BO.UserBreif;
import cn.sharp.bbs.entity.DO.Group;
import cn.sharp.bbs.service.UserFollowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserFollowServiceImpl implements UserFollowService {
    @Resource
    UserFollowDao userFollowDao;
    @Resource
    UserDao userDao;
    @Resource
    GroupMapper groupMapper;
    /**
     * 获取关注分组的列表
     * @param uid 当前用户id
     * @return 关注分组的列表
     */
    @Override
    public List<Group> getFollowLists(Long uid) {
        return userFollowDao.getFollowGroups(uid);
    }

    /**
     * 检查分组是否不属于某个用户
     * @param u_id 用户id
     * @param group 分组id
     * @return 布尔值, 分组是否不属于用户
     */
    @Override
    public boolean isInvalidGroup(Long u_id, Long group) {
        return userFollowDao.getFollowGroups(u_id)
                .stream().noneMatch(group1 -> group1.getId().equals(group));
    }

    /**
     * 获取分页的、某分组的关注用户列表
     * @param u_id 用户id
     * @param group 要查询的分组, 当为空时显示所有
     * @param pageParam 分页参数
     * @return 分页、分组的关注用户信息
     */
    @Override
    public Map<String,Object> getFollowInGroup(Long u_id, Long group, PageParam pageParam) {
        if(group==null){
            return Utils.getPage(pageParam, ()->userFollowDao.listFollowAll(u_id));
        }
        return Utils.getPage(pageParam, ()->userFollowDao.listUserInGroup(u_id, group));
    }

    /**
     * 获取最近的10个关注用户
     * @param account 用户账号
     * @return 近期关注列表
     */
    @Override
    public List<UserBreif> getRecentFollows(String account) {
        Long uid = userDao.selectUserByAccount(account).getId();
        return userFollowDao.getRecentFollows(uid);
    }

    /**
     * 将关注的用户复制至指定分组
     * @param u_id 操作人id
     * @param userIds 要复制的用户id
     * @param group 要复制到的目标分组
     * @return 报错内容, 无错为null
     */
    @Override
    public String copyFollowToGroup(Long u_id, Long[] userIds, Long group) {
        if(userIds.length>50) return "要移动的用户过多";
        List<Long> userIdList = Arrays.asList(userIds);
        if(userIdList.stream().anyMatch(userId->userDao.countFollowRelation(u_id, userId)==0))
            return "未关注用户不能加入分组";
        if(isInvalidGroup(u_id, group))
            return "目标分组错误";
        List<Long> followIds = userIdList.stream()
                .map(userId->userFollowDao.getFollowId(u_id, userId))
                .collect(Collectors.toList());
        followIds.stream().filter(followId->userFollowDao.countFollowInGroup(followId, group)==0)
                .forEach(followId->userFollowDao.addFollowToGroup(followId, group));
        return null;
    }

    /**
     * 获取用户所在的分组
     *
     * @param operator 操作人
     * @param u_id     用户id
     * @return 结果
     */
    @Override
    public List<Group> getUserInGroup(Long operator, Long u_id) {
        return userFollowDao.getFollowInGroup(operator, u_id);
    }

    /**
     * 设置关注关系的分组
     * @param operator 操作人id
     * @param u_id 要设置的关注用户
     * @param groups 要设置的分组的id
     * @return 设置结果
     */
    @Override
    public String setFollowToGroup(Long operator, Long u_id, Long[] groups) {
        List<Long> groupList = Arrays.asList(groups);
        if(groupList.stream().anyMatch(gid->isInvalidGroup(operator, gid)))
            return "目标分组错误";
        if(userDao.countFollowRelation(operator, u_id)==0)
            userDao.followUser(operator, u_id);
        Long followId = userFollowDao.getFollowId(operator, u_id);
        groupList.forEach(gid->{
            if(userFollowDao.countFollowInGroup(followId, gid)==0)
                userFollowDao.addFollowToGroup(followId, gid);
        });
        return null;
    }

    /**
     * 删除用户分组
     * @param u_id 用户id
     * @param group 用户分组
     * @return 删除结果, 无错为null
     */
    @Override
    public String delFollowGroup(Long u_id, Long group) {
        if(isInvalidGroup(u_id, group))
            return "无效的分组";
        userFollowDao.delGroup(group);
        return null;
    }

    /**
     * 新建用户分组
     * @param group 分组信息
     */
    @Override
    public void createGroup(Group group) {
        groupMapper.insert(group);
    }
}
